﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BrokerBase.Lib.Policy;
using MSWord = Microsoft.Office.Interop.Word;
using BrokerBase.Lib.Person;
using BrokerBase.Lib.Product;

namespace BrokerBase.Lib.InsurancePlan
{
    public class InsPlanBook
    {
        public const string Default_Template_FileName = "BookTemplate.xml";

        public InsPlanBook(IScheme scheme)
        {
            Scheme = scheme;
        }

        public IScheme Scheme { get; set; }

        public void CreateBook()
        {
            MSWord.Application wordApp = new Microsoft.Office.Interop.Word.Application();

            MSWord.Document doc = wordApp.Documents.Add();

            SetHeadAndFoot(Scheme, wordApp);

            object missing = System.Reflection.Missing.Value;

            object oEndOfDoc = "\\endofdoc";

            wordApp.Selection.ParagraphFormat.LineSpacing = 15; // 设置文档的行间距  

            MSWord.Paragraph par = doc.Paragraphs.Add(missing);
            par.Range.Text = "致谢函";
            par.Range.Font.Name = "黑体";
            par.Range.Font.Size = 18;
            par.Format.Alignment = MSWord.WdParagraphAlignment.wdAlignParagraphCenter;
            par.Range.InsertParagraphAfter();

            //公司简介
            //MSWord.Paragraph par1 = doc.Paragraphs.Add(missing);
            //par1.Range.Text = Scheme.Policies[0].Product.Company.Description;
            //par1.Range.Font.Name = "仿宋";
            //par1.Range.Font.Size = 12;
            //par1.Format.Alignment = MSWord.WdParagraphAlignment.wdAlignParagraphLeft;
            //par1.Range.InsertParagraphAfter();

            //称呼
            MSWord.Paragraph par2= doc.Content.Paragraphs.Add(missing);

            string insurantName = Scheme.Insurant == null ? string.Empty : Scheme.Insurant.Name;

            string s1 = "尊敬的" + insurantName + (Scheme.InsurantSex == SexType.Male ? "先生" : "女士") + ":";
            par2.Range.Text = s1;
            par2.Range.Font.Name = "仿宋";
            par2.Range.Font.Size = 12;
            par2.Format.Alignment = MSWord.WdParagraphAlignment.wdAlignParagraphLeft;
            par2.Range.InsertParagraphAfter();

            //前言
            MSWord.Paragraph par3 = doc.Content.Paragraphs.Add(missing);
            InsPlanDocTemplate tmp = InsPlanDocTemplate.Load(Default_Template_FileName);
            par3.Range.Text = tmp.Preface;
            par3.Range.InsertParagraphAfter();

            //基本信息
            MSWord.Paragraph p31 = doc.Paragraphs.Add(missing);
            p31.Range.Font.Name = "仿宋";
            p31.Range.Font.Size = 14;
            p31.Range.Font.Bold = 1;
            p31.Range.Text = "基本信息";
            p31.Range.InsertParagraphAfter();
            
            MSWord.Paragraph p33 = doc.Paragraphs.Add(missing);
            MSWord.Table baseTable;
            p33.Range.Font.Name = "仿宋";
            p33.Range.Font.Size = 12;
            baseTable = doc.Tables.Add(p33.Range, 2, 3, ref missing, ref missing);
            baseTable.Range.ParagraphFormat.SpaceAfter = 6;
            baseTable.Borders.InsideLineStyle = MSWord.WdLineStyle.wdLineStyleSingle;
            baseTable.Borders.OutsideLineStyle = MSWord.WdLineStyle.wdLineStyleSingle;

            baseTable.Columns[1].Width = 180;
            baseTable.Columns[2].Width = 160;
            baseTable.Columns[3].Width = 160;

            baseTable.Cell(1, 1).Range.Text = "投保人: " + (Scheme.Holder == null ? string.Empty : Scheme.Holder.Name);
            baseTable.Cell(1, 2).Range.Text = "性别: " + (Scheme.HolderSex == SexType.Male ? "男" : "女");
            baseTable.Cell(1, 3).Range.Text = "年龄: " + Scheme.HolderAge;

            baseTable.Cell(2, 1).Range.Text = "被保人: " + (Scheme.Insurant == null ? string.Empty : Scheme.Insurant.Name);
            baseTable.Cell(2, 2).Range.Text = "性别: " + (Scheme.InsurantSex == SexType.Male ? "男" : "女");
            baseTable.Cell(2, 3).Range.Text = "年龄: " + Scheme.InsurantAge;
            p33.Range.InsertParagraphAfter();


            //产品表格
            MSWord.Paragraph p4 = doc.Paragraphs.Add(missing);
            MSWord.Table oTable;
            p4.Range.Font.Name = "仿宋";
            p4.Range.Font.Size = 10;
            int row = Scheme.Policies.Count+1;
            oTable = doc.Tables.Add(p4.Range, row, 5, ref missing, ref missing);
            oTable.Range.ParagraphFormat.SpaceAfter = 6;
            
            oTable.Borders.InsideLineStyle = MSWord.WdLineStyle.wdLineStyleSingle;
            oTable.Borders.OutsideLineStyle = MSWord.WdLineStyle.wdLineStyleSingle;

            oTable.Columns[1].Width = 220;
            oTable.Columns[2].Width = 80;
            oTable.Columns[3].Width = 60;
            oTable.Columns[4].Width = 60;
            oTable.Columns[5].Width = 60;
            oTable.Cell(1, 1).Range.Text = "保险";
            oTable.Cell(1, 2).Range.Text = "保额/份数";
            oTable.Cell(1, 3).Range.Text = "保险期限";
            oTable.Cell(1, 4).Range.Text = "付款期限";
            oTable.Cell(1, 5).Range.Text = "缴费方式";
            p4.Range.InsertParagraph();

            for (int i = 0; i < Scheme.Policies.Count; i++)
            {
                IPolicy policy=Scheme.Policies[i];
                oTable.Cell(i + 2, 1).Range.Text = policy.Product.Name;
                
                oTable.Cell(i + 2, 2).Range.Text = policy.Amount.ToString();
                oTable.Cell(i + 2, 3).Range.Text = policy.SelectedInsurancePeriod.Name;
                oTable.Cell(i + 2, 4).Range.Text = policy.SelectedPaymentPeriod.Name;
                oTable.Cell(i + 2, 5).Range.Text = policy.SelectedTimeUnit.Name;
                
            }

            //利益
            MSWord.Paragraph p5 = doc.Paragraphs.Add(missing);
            p5.Range.Font.Name = "仿宋";
            p5.Range.Font.Size = 14;
            p5.Range.Font.Bold = 1;
            p5.Range.Text = "利益";
            p5.Range.InsertParagraphAfter();

            foreach (IPolicy policy in Scheme.Policies)
            {
                MSWord.Paragraph ph = doc.Paragraphs.Add(missing);

                ph.Range.Font.Name = "黑体";
                ph.Range.Font.Size = 14;
                ph.Range.Font.Bold = 1;
                ph.Range.Text = policy.Product.Name;
                ph.Range.InsertParagraphAfter();

                foreach (IDuty duty in policy.Product.Duties)
                {
                    MSWord.Paragraph pd = doc.Paragraphs.Add(missing);
                    pd.Range.Font.Name = "仿宋";
                    pd.Range.Font.Size = 13;
                    pd.Range.Font.Bold = 1;
                    pd.Range.Text = duty.Name; ;
                    pd.Range.InsertParagraphAfter();

                    MSWord.Paragraph pd2 = doc.Paragraphs.Add(missing);
                    pd2.Range.Font.Name = "仿宋";
                    pd2.Range.Font.Size = 12;
                    pd2.Range.Font.Bold = 0;
                    pd2.Range.Text = duty.Description;
                    pd2.Range.InsertParagraphAfter();
                }
            }


            wordApp.Visible = true;

        }

        /// <summary>
        /// 设置页眉和页脚
        /// </summary>
        /// <param name="scheme"></param>
        /// <param name="wordApp"></param>
        private void SetHeadAndFoot(IScheme scheme, MSWord.Application wordApp)
        {
            wordApp.ActiveWindow.View.Type = MSWord.WdViewType.wdOutlineView;
            wordApp.ActiveWindow.View.SeekView = MSWord.WdSeekView.wdSeekPrimaryHeader;
            wordApp.ActiveWindow.ActivePane.Selection.InsertAfter("美联达");
            wordApp.Selection.ParagraphFormat.Alignment = MSWord.WdParagraphAlignment.wdAlignParagraphRight; // 设置右对齐   

            wordApp.ActiveWindow.View.SeekView = MSWord.WdSeekView.wdSeekCurrentPageFooter;

            string company = scheme.Policies[0].Product.Company.FullName;

            wordApp.ActiveWindow.ActivePane.Selection.InsertAfter(company);
            wordApp.Selection.ParagraphFormat.Alignment = MSWord.WdParagraphAlignment.wdAlignParagraphRight; // 设置右对齐   

            wordApp.ActiveWindow.View.SeekView = MSWord.WdSeekView.wdSeekMainDocument; // 跳出页眉设置   


        }

        private void SetPreface(MSWord.Document wordDoc)
        {

        }

    }
}
